package com.knowswift.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

@Slf4j
public class ListUtil {
    public static <S, T> List<T> transformOtherTypeList(List<S> sourceList, Class<T> clazz, BiFunction<S, T, T> afterCopyAction, String... ignoreProperties) {
        List<T> result = new ArrayList<>();
        if (sourceList != null) {
            try {
                for (S record : sourceList) {
                    T t = clazz.newInstance();
                    BeanUtils.copyProperties(record, t, ignoreProperties);
                    if (afterCopyAction != null) {
                        t = afterCopyAction.apply(record, t);
                    }
                    result.add(t);
                }
            } catch (InstantiationException | IllegalAccessException e) {
                log.error("Error while transforming the List of Class: " + clazz.getName() + ", error:" + e.getMessage());
            }
        }
        return result;
    }
}
